package com.yanqu.road.dao.impl.activity.sonhai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiBeastBossData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class SonHaiRegionDataDaoImpl extends TempDataObjectDao<SonHaiRegionData> {

    public static String getSonHaiRegionDataSql = "select * from `t_c_activity_son_hai_region_data` where `activityId`=? and groupId = ? and delFlag = 0";
    public Map<Integer, Map<Integer, SonHaiRegionData>> getSonHaiRegionData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        //k:队伍ID,k:区域ID
        Map<Integer, Map<Integer, SonHaiRegionData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSonHaiRegionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                SonHaiRegionData data = parse(rs);
                Map<Integer, SonHaiRegionData> regionMap = map.computeIfAbsent(data.getTeamId(), k -> new ConcurrentHashMap<>());
                regionMap.put(data.getRegionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSonHaiRegionDataSql = "update `t_c_activity_son_hai_region_data` set progress=?,beastBoss=?,mapId=?,delFlag=?,createTime=?,launchRestUserId=?,agreeRestUserIds=?,autoRestTime=? where activityId = ? and groupId = ? and teamId = ? and regionId = ? and id = ?";
    @Override
    protected boolean updateData(SonHaiRegionData sonHaiRegionData) {
        sonHaiRegionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getProgress()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiRegionData.getBeastBoss())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getMapId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiRegionData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiRegionData.getLaunchRestUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiRegionData.getAgreeRestUserIds())));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiRegionData.getAutoRestTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getId()));
        if (getDb().executeNoneQuery(updateSonHaiRegionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSonHaiRegionDataSql = "insert into `t_c_activity_son_hai_region_data`(activityId,groupId,id,teamId,regionId,mapId,progress,beastBoss,delFlag,createTime,launchRestUserId,agreeRestUserIds,autoRestTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SonHaiRegionData sonHaiRegionData) {
        sonHaiRegionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getMapId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getProgress()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiRegionData.getBeastBoss())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiRegionData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiRegionData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiRegionData.getLaunchRestUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiRegionData.getAgreeRestUserIds())));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiRegionData.getAutoRestTime()));
        if (getDb().executeNoneQuery(insertSonHaiRegionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SonHaiRegionData parseData(ResultSet rs) throws SQLException {
        SonHaiRegionData sonHaiRegionData = new SonHaiRegionData();
        sonHaiRegionData.setActivityId(rs.getInt("activityId"));
        sonHaiRegionData.setGroupId(rs.getInt("groupId"));
        sonHaiRegionData.setId(rs.getInt("id"));
        sonHaiRegionData.setTeamId(rs.getInt("teamId"));
        sonHaiRegionData.setRegionId(rs.getInt("regionId"));
        sonHaiRegionData.setMapId(rs.getInt("mapId"));
        sonHaiRegionData.setProgress(rs.getInt("progress"));
        sonHaiRegionData.setBeastBoss(JSON.parseObject(rs.getString("beastBoss"), SonHaiBeastBossData.class));
        sonHaiRegionData.setDelFlag(rs.getInt("delFlag"));
        sonHaiRegionData.setCreateTime(rs.getLong("createTime"));
        sonHaiRegionData.setLaunchRestUserId(rs.getLong("launchRestUserId"));
        if (!StringUtils.isNullOrEmpty(rs.getString("agreeRestUserIds"))) {
            sonHaiRegionData.setAgreeRestUserIds(JSON.parseObject(rs.getString("agreeRestUserIds"), new TypeReference<Set<Long>>() {}));
        }
        sonHaiRegionData.setAutoRestTime(rs.getLong("autoRestTime"));
        return sonHaiRegionData;
    }
}
